package com.zp.self;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by zengpeng on 2020/12/07
 */
@SpringBootApplication
@RestController
@NacosPropertySource(dataId = "nacos-client-a", autoRefreshed = true)
@EnableDiscoveryClient
//@RefreshScope  //刷新配置
public class NacosApplication {
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Resource
    NacosService nacosService;

    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class, args);
    }

    @NacosValue(value = "${useLocalCache:false}" ,autoRefreshed = true)
    private boolean useLocalCache;

    //测试动态获取配置
    @RequestMapping(value = "/get", method = GET)
    @ResponseBody
    public boolean get() {
        return useLocalCache;
    }

    //测试Rpc远程访问
    @GetMapping("/nacos-test")
    public Object hystrix(String name){
        return nacosService.rpcRequest(name);
    }

}